// pages/dayCash/index.js
const common = require("../../public.js");
Page({
  data: {
    surplusMoney:'',
    succMoney:'',
    receiveInfoJson:{},
    surplusMoney:'',
    currentTimestamp: null, // 当前日期的时间戳
    dayList: [],
    weekList: ["日", "一", "二", "三", "四", "五", "六"],
    orderNo:'',
    userInfo: {},
    token: '',
    currentYear:'',
    currentMonth:'',
    currentDay:'',
    todayGet:false,// 今日领取true 今日未领取 false
  },

  onLoad: function (options) {
    const that = this
    common.getLocalUserInfo(function (success, data) {
      if (success) {
        that.setData({
          userInfo: data.user,
          token: data.token,
          orderNo:options.orderNo?options.orderNo:'1701940725761967502' //1702018251835067956   
        }, function () {
            this.getTimeFunc();
            // {{receiveInfoJson.succMoney}}
            this.getDayCashFunc();
        })
      }
    })


  },

  onShow: function () {

  },
  getDayCashFunc(){

        let that = this
        let tempJson = {
          userId: that.data.userInfo.id,
          token: that.data.token,
          orderNo: that.data.orderNo
        }
        console.log('====g===',tempJson);
        // return;
        common.post("/getUserZeroOrderReceiveInfo", tempJson, function (res)  {
            console.log('getUserZeroOrderReceiveInfo----',res);
            if (res.status == 200) {
                that.setData({
                    receiveInfoJson:res.data,
                    succMoney:res.data?parseInt(res.data.succMoney*100):{},
                    surplusMoney:res.data?parseInt(res.data.surplusMoney*100):{},
                    
                    dayList:[],
                },()=>{
                    that.bindData(res.data.receiveRecord);
                })
               


            } else {
             
            }
          }, function () {
           
          })
        },
  clickSave() {
    const benUrl = 'https://feiyangimage.oss-cn-shanghai.aliyuncs.com/wxApplets/serverQrcode.png'
    wx.getImageInfo({
      src: benUrl,
      success: function (res) {
        var path = res.path
        wx.saveImageToPhotosAlbum({
          filePath: path,
          success(res) {
            // common.showToast('保存成功')
          }
        })
      }
    })
  },
  bindData(resData) {
      var that = this;
    var netPriceData = resData;
    console.log('-原始---netPriceData---',netPriceData);
    if(netPriceData.length){
            this.setData({
                todayGet:netPriceData[netPriceData.length-1].rflag==1?true:false
            })
    }
    var date = new Date();
    console.log("当前年份", date.getFullYear()); //获取完整的年份(4位)
    this.data.currentYear = date.getFullYear();
    console.log("当前月份", date.getMonth() + 1);
    this.data.currentMonth = date.getMonth();
    console.log("当前日", date.getDate());
    this.data.currentDay = date.getDate();
    var weekNum = this.weekNumFunc();
    console.log("当月1号是星期", weekNum);
    var days = this.getMonthDay(this.data.currentYear, this.data.currentMonth); // 这个月有多少天

    console.log(this.data.currentMonth + 1 + "月aaaa有", days, "天");

    console.log("总数为", parseInt(days) + parseInt(weekNum));

    // 构造不带价格的数据源

    for (var i = 0; i < parseInt(days) + parseInt(weekNum); i++) {
      if (i < parseInt(weekNum)) {
        //  0 1 2 3 4
        console.log("~~~~~", i);
        this.data.dayList.push({});
      } else {
        console.log("+++++++", i);

        this.data.dayList.push({
          year: this.data.currentYear,
          month: this.data.currentMonth+1,
          num: 1 + i - parseInt(weekNum)// 1号 2号  3号......
        });
      }
    }



    //不带价格的数据源与网络请求到的带有价格的数据源处理
    console.log("日期总数列表为", this.data.dayList,'字符串json为',JSON.stringify(this.data.dayList));



    //  var oneNum = false; // 网络数据中的第一个数据不是01号

    // if (netPriceData.length) {
    //     if(netPriceData[0].receiveDate){
    //        var changeTimeDay = this.timestampToDay(netPriceData[0].receiveDate);
    //        console.log("第一个时间为", changeTimeDay);
    //     }
    
 
    //  }

    var run = false;
    var iValue;
    for (var i = 0; i < this.data.dayList.length; i++) {
      if (i < parseInt(weekNum)) {
        //  0 1 2 3 4
      } else {
        console.log("+++++++", i);

         var difference = this.data.dayList.length - netPriceData.length;
        console.log("difference为", difference);
        // if(this.data.dayList[i].num == changeTimeDay){
        //     console.log('满足啦----- i为',i);
        //     run = true;
        //     iValue = i;
        // }

        if(run == true){
            console.log('索引---',i - iValue,"====i为",i ,"iValue---为",iValue,"--类型为",typeof(i - iValue),'---netPriceData[i - iValue]为',netPriceData[i - iValue]);
        // console.log('todayPrice为',netPriceData[i - iValue].todayPrice);

            if(!netPriceData[i - iValue]){
              console.log('不再执行拉');
              return;
            }
            // this.data.dayList[i].rflag = netPriceData[i - iValue].rflag;

        //   this.data.dayList[i].receiveDate = netPriceData[i - iValue].receiveDate;
        
        }


      }
    }

     console.log('最终数据为====',JSON.stringify(this.data.dayList));

    //   this.data.dayList.length%7
      for (var i = 0; i < this.data.dayList.length%7; i++) {
         this.data.dayList.push({});
    }
    // netPriceData[netPriceData.length-1].rflag
    for(var i = 0;i <that.data.dayList.length;i++){
        var item1 = that.data.dayList[i];
        console.log('item1--',item1);
        if(!item1.month){
            continue;
        }
        var month = item1.month<10?('0'+item1.month):item1.month;
        var day = item1.num<10?('0'+item1.num):item1.num;
        var ymd = item1.year + '' + month + '' +day;
        console.log('ymd--',ymd);
        for(var j = 0;j <netPriceData.length;j++){
            var item2 = netPriceData[j];
            var netymd = item2.receiveDate.replace(/-/g,"");  
            console.log('netymd--',netymd);
            if(ymd == netymd){
                console.log('有相等的');
              
                item1.select = item2.rflag==1?2:3;
            }
           
          
        }
    }

     this.setData({
        dayList:this.data.dayList,
        currentYear:this.data.currentYear,
        currentMonth:this.data.currentMonth+1,
        currentDay:this.data.currentDay
     },()=>{

        console.log("dayList为", this.data.dayList);
     })

  },

    // 获取一个月有多少天的方式
    getMonthDay(year, month) {
        let days = new Date(year, month + 1, 0).getDate();
        return days;
      },

      dayClick(){
        let {
            item
          } = e.currentTarget.dataset
          console.log('当前item---',item);
      },

      getTimeFunc(){
        var nowDate = new Date();
        var year = nowDate.getFullYear();
    
        var month = nowDate.getMonth();
    
        var day = nowDate.getDate();
        var timeaaa =
          year + "-" + this.dealTen(month + 1) + "-" + this.dealTen(day);
        console.log("timeaaa为", timeaaa);
        var date = new Date(
          year +
            "-" +
            this.dealTen(month + 1) +
            "-" +
            this.dealTen(day) +
            " 00:00:00"
        );
        this.data.currentTimestamp = date.getTime();
        console.log("时间戳为", this.data.currentTimestamp);
      },
      dealTen(source) {
        return source < 10 ? "0" + source : source;
      },
            // 判断当月1号是星期几
            weekNumFunc() {
                //JS判断的if elseif方法
                var wstr = "";
                var date = new Date();
                console.log("啊啊啊啊阿", date);
                date.setDate(1);
                var weekday = new Array(7);
                weekday[0] = "7";
                weekday[1] = "1";
                weekday[2] = "2";
                weekday[3] = "3";
                weekday[4] = "4";
                weekday[5] = "5";
                weekday[6] = "6";
                console.log("本月第一天是 " + weekday[date.getDay()]);
                wstr = weekday[date.getDay()];
                console.log("本月第一天是--- " + weekday[date.getDay()]);
                console.log("本月第一天是+++ " + wstr);
                return wstr;
                // date.setMonth(date.getMonth() + 1);
                // var lastDate = new Date(date - 3600000 * 24);
                // console.log("本月最后一天是 " + lastDate.getDate());
                // wstr = weekday[date.getDay()];
                //  console.log("本月第一天是--- " + weekday[date.getDay()]);
                //   console.log("本月第一天是+++ " + wstr);
                // return wstr;
              },



              receiveMoneyForDayFunc(){

                let that = this
                let tempJson = {
                  userId: that.data.userInfo.id,
                  token: that.data.token,
                  orderNo: that.data.orderNo
                }
                console.log('====g===',tempJson);
                // return;
                common.post("/receiveMoneyForDay", tempJson, function (res)  {
                    if (res.status == 200) {
                        wx.showToast({
                            title: '领取成功',
                            icon: "none"
                        })
                      
                        that.getDayCashFunc();
                    } else {
                        wx.showToast({
                            title: res.msg ? res.msg : '领取失败',
                            icon: "none"
                        })
                    }
                  }, function () {
                   
                  })
                },

                  
      timestampToDay(timestamp) {
        var date = new Date(timestamp); // 时间戳为10位需*1000，时间戳为13位的话不需乘1000
        let d = date.getDate();
        // d = d < 10 ? "0" + d : d;
   
  
        return d;
      },
  
})